"""系统配置设置"""

from typing import Optional

from dotenv import load_dotenv
from pydantic import Field
from pydantic_settings import BaseSettings as PydanticBaseSettings

load_dotenv()


class Settings(PydanticBaseSettings):
    """系统主配置"""

    # 基础配置
    app_name: str = Field(default="LlamaIndex Data Analysis Platform", description="应用名称")
    debug: bool = Field(default=False, description="调试模式")
    log_level: str = Field(default="INFO", description="日志级别")
    dashscope_apikey: str = Field(default="sk-b5a1d95d5595453dbafb47db0a80d4be", description="Dashscope API key")

    # 数据库配置
    postgres_host: str = Field(default="odoo-temp.pg.polardb.rds.aliyuncs.com", description="PostgreSQL主机")
    postgres_port: int = Field(default=5432, description="PostgreSQL端口")
    postgres_db: str = Field(default="vector_db", description="PostgreSQL数据库名")
    postgres_user: str = Field(default="p_dispatcher_rw", description="PostgreSQL用户名")
    postgres_password: str = Field(default="5QFCCoDlkBgYHW63zBFu", description="PostgreSQL密码")

    # Minio配置
    minio_endpoint: str = Field(default="8.147.112.186:9000", description="Minio端点")
    minio_access_key: str = Field(default="minioadmin", description="Minio访问密钥")
    minio_secret_key: str = Field(default="minioadmin123", description="Minio秘密密钥")
    minio_secure: bool = Field(default=False, description="Minio是否使用HTTPS")
    minio_bucket: str = Field(default="data", description="Minio存储桶")

    # neo4j配置
    neo4j_username: str = Field(default="neo4j", description="Neo4j用户名")
    neo4j_password: str = Field(default="xubo1996", description="Neo4j密码")
    neo4j_db: str = Field(default="neo4j", description="Neo4j database")
    neo4j_url: str = Field(default="bolt://8.147.112.186:7687", description="Neo4j url")

    # Kafka配置
    kafka_bootstrap_servers: str = Field(default="8.147.112.186:9092", description="Kafka服务器地址")
    kafka_topic_uploads: str = Field(default="file-updates", description="文件上传主题")
    kafka_client_id: str = Field(default="data-analysis-platform", description="Kafka客户端ID")

    chunk_size: int = Field(default=1024, description="文档块大小")
    chunk_overlap: int = Field(default=128, description="文档块重叠")

    @property
    def postgres_url(self) -> str:
        """PostgreSQL连接字符串"""
        return f"postgresql://{self.postgres_user}:{self.postgres_password}@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}"

    @property
    def async_postgres_url(self) -> str:
        """异步PostgreSQL连接字符串"""
        return f"postgresql+asyncpg://{self.postgres_user}:{self.postgres_password}@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}"

    class Config:
        env_file = ".env"
        case_sensitive = False


# 全局配置实例
settings = Settings()
